package com.papper.mng.controller.biz;

import com.google.common.collect.Maps;
import com.papper.common.bean.biz.BizAccountGroup;
import com.papper.common.bean.biz.BizAccountInfo;
import com.papper.common.config.StorageConstants;
import com.papper.common.enums.DataStatusEnum;
import com.papper.common.exception.ServiceException;
import com.papper.common.service.biz.BizAccountGroupService;
import com.papper.common.service.biz.BizAccountInfoService;
import com.papper.common.utils.encrypt.AESTool;
import com.papper.mng.controller.BaseAction;
import com.papper.mng.domain.DwzResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author:LaJiao
 * @Date:2022-04-21 11:04:21
 * @Description:个人账号信息
 */
@Controller
@RequestMapping("/biz/account/info")
public class BizAccountInfoController extends BaseAction {

    @Autowired
    BizAccountInfoService  bizAccountInfoService;
    @Autowired
    BizAccountGroupService bizAccountGroupService;

    /**
     * 个人账号信息
     *
     * @return
     */
    @RequestMapping(value = "")
    public String execute(HttpServletRequest _request, Model model, Long groupId) {
        // 获取组信息
        List<BizAccountGroup> groupList = bizAccountGroupService.findAll();
        model.addAttribute("groupList", groupList);
        if (groupList == null) {
            return render("biz/account/info/index");
        }
        BizAccountGroup accountGroup = bizAccountGroupService.findById(groupId);
        if (accountGroup == null) {
            accountGroup = groupList.get(0);
            groupId = accountGroup.getId();
        }
        // 字段查询条件
        Map<String, Object> objects = Maps.newHashMap();
        extraParams(objects, _request, BizAccountInfo.class);
        objects.put("groupId", groupId);
        modelParm(objects, model);
        model.addAttribute("groupId", groupId);
        model.addAttribute("accountGroup", accountGroup);
        // 数据查询组装
        Example              example  = buildExample(objects, model, BizAccountInfo.class);
        List<BizAccountInfo> infoList = bizAccountInfoService.find(example);
        infoList.forEach(info -> {
            try {
                info.setAccountPass(AESTool.decode(info.getAccountPass(), StorageConstants.getAesKey()));
                info.setAccountPass(AESTool.encode(info.getAccountPass(), StorageConstants.getPageAesKey()));
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        model.addAttribute("records", infoList);
        return render("biz/account/info/index");
    }

    /**
     * 个人账号信息_新增
     */
    @GetMapping(value = "/add")
    public String add_get(Model model, Long groupId) {
        model.addAttribute("groupId", groupId);
        return render("biz/account/info/add");
    }

    @PostMapping(value = "/add")
    @ResponseBody
    public DwzResponse add_post(HttpServletRequest _request, BizAccountInfo bizAccountInfo) {
        try {
            checkVerifyCert(_request);
        } catch (ServiceException e) {
            return new DwzResponse("300", e.getMessage());
        }

        // 添加个人账号信息
        try {
            bizAccountInfo.setAccountPass(AESTool.encode(bizAccountInfo.getAccountPass(), StorageConstants.getAesKey()));
            bizAccountInfo.setDataStatus(DataStatusEnum.OPEN.getCode());
            bizAccountInfo.setCreateTime(new Date());
            bizAccountInfo.setModifyTime(new Date());
            bizAccountInfoService.save(bizAccountInfo);
            return new DwzResponse("200", "个人账号信息添加成功!!!", "closeCurrent");
        } catch (Exception e) {
            e.printStackTrace();
            return new DwzResponse("300", "个人账号信息添加失败:" + e.getMessage() + "!!!");
        }
    }

    /**
     * 个人账号信息_更新
     */
    @GetMapping(value = "/edit")
    public String edti_get(Model model, Long id) {
        BizAccountInfo entity = bizAccountInfoService.findById(id);
        model.addAttribute("entity", entity);
        return render("biz/account/info/edit");
    }

    @PostMapping(value = "/edit")
    @ResponseBody
    public DwzResponse edit_post(HttpServletRequest _request, BizAccountInfo bizAccountInfo) {
        try {
            checkVerifyCert(_request);
        } catch (ServiceException e) {
            return new DwzResponse("300", e.getMessage());
        }
        if (bizAccountInfo == null || bizAccountInfo.getId() == null) {
            return new DwzResponse("300", "参数异常！");
        }
        try {
            bizAccountInfo.setModifyTime(new Date());
            bizAccountInfoService.update(bizAccountInfo);
            return new DwzResponse("200", "个人账号信息修改成功!!!", "closeCurrent");
        } catch (Exception e) {
            e.printStackTrace();
            return new DwzResponse("300", "个人账号信息修改失败:" + e.getMessage());
        }
    }

    /**
     * 个人账号信息_更新
     */
    @GetMapping(value = "/pass")
    public String pass_get(Model model, Long id) {
        BizAccountInfo entity = bizAccountInfoService.findById(id);
        model.addAttribute("entity", entity);
        return render("biz/account/info/pass");
    }

    @PostMapping(value = "/pass")
    @ResponseBody
    public DwzResponse pass_post(HttpServletRequest _request, BizAccountInfo bizAccountInfo) {
        try {
            checkVerifyCert(_request);
        } catch (ServiceException e) {
            return new DwzResponse("300", e.getMessage());
        }
        if (bizAccountInfo == null || bizAccountInfo.getId() == null) {
            return new DwzResponse("300", "参数异常！");
        }
        try {
            bizAccountInfo.setAccountPass(AESTool.encode(bizAccountInfo.getAccountPass(), StorageConstants.getAesKey()));
            bizAccountInfo.setModifyTime(new Date());
            bizAccountInfoService.update(bizAccountInfo);
            return new DwzResponse("200", "个人账号信息修改成功!!!", "closeCurrent");
        } catch (Exception e) {
            e.printStackTrace();
            return new DwzResponse("300", "个人账号信息修改失败:" + e.getMessage());
        }
    }

    /**
     * 个人账号信息_删除
     *
     * @param id
     * @
     */
    @PostMapping(value = "/del")
    @ResponseBody
    public DwzResponse del_post(HttpServletRequest _request, Long id) {
        try {
            checkVerifyCert(_request);
        } catch (ServiceException e) {
            return new DwzResponse("300", e.getMessage());
        }
        if (id == null || 0 >= id.intValue()) {
            return new DwzResponse("300", "参数ID为空！");
        }
        BizAccountInfo old = bizAccountInfoService.findById(id);
        if (old == null) {
            return new DwzResponse("300", "参数[id]信息异常！");
        }
        try {
            bizAccountInfoService.deleteById(id);
            return new DwzResponse("200", "个人账号信息删除成功!!!");
        } catch (Exception e) {
            e.printStackTrace();
            return new DwzResponse("300", "个人账号信息删除失败:" + e.getMessage());
        }
    }

    /**
     * 个人账号信息_详情
     *
     * @return
     */
    @GetMapping(value = "/detail")
    public String detail(HttpServletResponse _response, Model model, Long id) {
        BizAccountInfo bizAccountInfo = bizAccountInfoService.findById(id);
        model.addAttribute("entity", bizAccountInfo);
        return render("biz/account/info/detail");
    }
}
